package com.mxpio.erp.hr.controller;


import com.mxpio.erp.common.enums.BizExceptionEnums;
import com.mxpio.erp.common.exception.BizException;
import com.mxpio.erp.common.hr.entity.Contract;
import com.mxpio.erp.common.hr.service.ContractService;
import com.mxpio.erp.common.util.AssertUtil;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ContractController", description = "员工合同")
@RestController
@RequestMapping("/erp/hr/contract/")
public class ContractController {

    @Resource
    private ContractService contractService;

    @GetMapping("list")
    @Operation(summary = "员工合同列表", description = "员工合同列表（无分页）", method = "GET")
    public Result<List<Contract>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        try {
            List<Contract> list = contractService.list(Contract.class, criteria);
            return Result.OK(list);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("page")
    @Operation(summary = "员工合同列表", description = "员工合同列表（分页）", method = "GET")
    public Result<Page<Contract>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        try {
            Pageable page = PageRequest.of(pageNo - 1, pageSize);
            Page<Contract> pageResult = contractService.listPage(Contract.class, page, criteria);
            return Result.OK(pageResult);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取员工合同", description = "根据ID获取员工合同", method = "GET")
    public Result<Contract> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        try {
            Contract contract = contractService.getById(Contract.class, bizNo);
            return Result.OK(contract);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @RequestMapping("add")
    @Operation(summary = "保存员工合同", description = "保存员工合同")
    public Result<Contract> add(@RequestBody Contract contract) {
        try {
            return Result.OK(contractService.doSave(contract));
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<Contract> edit(@RequestBody Contract contract) {
        try {
            AssertUtil.notNull(contract, BizExceptionEnums.PARAM_NOT_NULL);
            AssertUtil.notEmpty(contract.getBizNo(), BizExceptionEnums.PARAM_NOT_EMPTY, "bizNo");
            Contract dbData = contractService.getById(Contract.class, contract.getBizNo());
            AssertUtil.notNull(dbData, BizExceptionEnums.ORDER_UNABLE_DELETE_BY_STATUS, contract.getBizNo());
            contractService.update(contract);
            return Result.OK(contract);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<Contract> remove(@PathVariable(name = "ids", required = true) String ids) {
        try {
            String[] id = ids.split(",");
            for (String key : id) {
                contractService.delete(Contract.class, key);
            }
            return Result.OK();
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

}